import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:getwidget/getwidget.dart';
import 'package:sz_metro/page/code/index.dart';
import 'package:sz_metro/page/home/index.dart';
import 'package:sz_metro/page/life/index.dart';
import 'package:sz_metro/page/mine/index.dart';
import 'package:sz_metro/page/trip/index.dart';
import 'package:flutter_app_badger/flutter_app_badger.dart';
import 'package:sz_metro/utils/platform.dart';

class TabBarContent extends StatefulWidget {
  const TabBarContent({Key? key}) : super(key: key);

  @override
  State<TabBarContent> createState() => _TabBarContentState();
}

class _TabBarContentState extends State<TabBarContent>
    with SingleTickerProviderStateMixin {
  late TabController tabController;
  String _appBadgeSupported = 'Unknown';

  @override
  void initState() {
    if (PlatformUtils.isAndroid ==true || PlatformUtils.isIOS == true ){
       initPlatformState();
    }
    super.initState();

    tabController = TabController(length: 5, vsync: this);
  }

  initPlatformState() async {
    String appBadgeSupported;
    try {
      bool res = await FlutterAppBadger.isAppBadgeSupported();
      if (res) {
        appBadgeSupported = 'Supported';
      
      } else {
        appBadgeSupported = 'Not supported';
      }
      // ignore: nullable_type_in_catch_clause
    } on PlatformException {
      appBadgeSupported = 'Failed to get badge support.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _appBadgeSupported = appBadgeSupported;
    });
  }

  @override
  void dispose() {
    tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[200],
      body: GFTabBarView(controller: tabController, children: const <Widget>[
        HomeContent(),
        TripContent(),
        CodeContent(),
        LifeContent(),
        MineContent(),
      ]),
      bottomNavigationBar: GFTabBar(
        labelColor: Colors.green,
        unselectedLabelColor: Colors.grey,
        tabBarColor: Colors.white,
        indicatorWeight: 0.01,
        indicatorSize: TabBarIndicatorSize.tab,
        length: 3,
        controller: tabController,
        tabs: const [
          Tab(
            icon: Icon(Icons.home),
            child: Text(
              '首页',
              style: TextStyle(fontSize: 11),
            ),
          ),
          Tab(
            icon: Icon(Icons.train),
            child: Text(
              '出行',
              style: TextStyle(fontSize: 11),
            ),
          ),
          Tab(
            icon: Icon(
              Icons.qr_code,
            ),
            child: Text(
              '乘车码',
              style: TextStyle(fontSize: 11),
            ),
          ),
          Tab(
            icon: Icon(Icons.heart_broken),
            child: Text(
              '生活',
              style: TextStyle(fontSize: 11),
            ),
          ),
          Tab(
            icon: Icon(Icons.person),
            child: Text(
              '我的',
              style: TextStyle(fontSize: 11),
            ),
          ),
        ],
      ),
    );
  }
}
